<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('角色列表')"/>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <label class="layui-form-label">角色名</label>
                <div class="layui-input-inline">
                    <input type="text" name="param" placeholder="请输入角色名" class="layui-input">
                </div>
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="role-query">
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button type="reset" class="pear-btn pear-btn-md">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="role-table" lay-filter="role-table"></table>
    </div>
</div>
</body>

<script type="text/html" id="role-toolbar">
    <button  class="pear-btn pear-btn-primary pear-btn-md" lay-event="save">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="delete">
        <i class="layui-icon layui-icon-delete"></i>
        删除
    </button>
</script>

<script type="text/html" id="role-bar">

    <button  class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>
    </button>
    <button  class="pear-btn pear-btn-warming pear-btn-sm" lay-event="author"><i class="layui-icon layui-icon-vercode"></i>
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="delete"><i class="layui-icon layui-icon-delete"></i>
    </button>

</script>

<script type="text/html" id="role-enable">
    <input type="checkbox" name="enable" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="role-enable" {{d.stauts === 0 ? 'checked' : '' }} >
</script>

<th:block th:include="include :: footer"/>
<script>
    layui.use(['table', 'form', 'jquery'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let MODULE_PATH = rootPath;

        let cols = [
            [
                {type: 'checkbox'},
                {title: 'ID', field: 'id', align: 'center'},
                {title: '角色名', field: 'name', align: 'center'},
                {title: '操作', toolbar: '#role-bar', align: 'center'}
            ]
        ]

        table.render({
            elem: '#role-table',
            url: rootPath + '/roles',
            page: true,
            method: 'GET',
            cols: cols,
            skin: 'line',
            toolbar: '#role-toolbar',
            defaultToolbar: [{
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'exports'],
            //重新定义请求
            request:{
                pageName:'pageNo',//每页数据
                limitName:"pageSize"//每页数据量
            },
            //重新定义解析接口返回数据
            parseData:function (res){
                return {
                    "code":res.code,
                    "msg":res.msg,
                    "count":res.count,
                    "data":res.data
                }
            }
        });

        //table图标事件绑定
        table.on('tool(role-table)', function (obj) {
            switch (obj.event){
                case 'delete':
                    let ids = [];
                    ids.push(obj.data.id);
                    eventStatus.delete(ids);
                    break;
                case 'edit':
                    eventStatus.edit(obj);
                    break;
                case 'author':
                    eventStatus.author(obj);
                    break;
            }
        });

        //table工具栏绑定
        table.on('toolbar(role-table)', function (obj) {

            switch (obj.event){
                case 'save':
                    eventStatus.save();
                    break;
                case 'refresh':
                    eventStatus.refresh();
                    break;
                case 'delete':
                    let data = table.checkStatus(obj.config.id).data;
                    var ids = [];
                    for (let i = 0; i < data.length; i++) {
                        ids.push(data[i].id);
                    }
                    eventStatus.delete(ids);
            }
        });


        form.on('submit(role-query)', function (data) {
            table.reload('role-table', {where: data.field})
            return false;
        });

        //获取各种方法集合
        let eventStatus = {
            save : function (){
                layer.open({
                    type: 2,
                    title: '新增',
                    shade: 0.1,
                    area: ['500px', '500px'],
                    content: MODULE_PATH + '/role/save'
                });
            },
            delete : function (ids){
                if (ids.length === 0) {
                    layer.msg("未选中数据", {icon: 3, time: 1000});
                    return false;
                }
                var userObj  = {"ids" : ids};

                layer.confirm('确定要删除这些用户', {icon: 3, title: '提示'}, function (index) {
                    layer.close(index);
                    let loading = layer.load();
                    Util.postJSON(MODULE_PATH + "/role/delete", userObj).
                    done(function (res){
                        layer.close(loading);
                        if (res.code === 0){
                            layer.msg('已删除', {icon: 1, time: 1000}, function () {
                                table.reload('role-table');
                            });
                        } else {
                            layer.msg(res.message || '删除失败', {icon: 2, time: 1000});
                        }
                    }).
                    fail(function (res){
                        layer.close(loading);
                        layer.msg(res.message || '删除失败', {icon: 2, time: 1000});
                    })
                })
            },
            edit : function (obj){
                layer.open({
                    type: 2,
                    title: '修改',
                    shade: 0.1,
                    area: ['500px', '500px'],
                    content: MODULE_PATH + '/role/edit/' + obj.data['id']
                });
            },
            refresh : function (){
                table.reload('user-table');
            },
            author: function (obj){
                layer.open({
                    type: 2,
                    title: '授权',
                    shade: 0.1,
                    area: ['320px', '400px'],
                    content: rootPath + '/permission/tree/' + obj.data["id"]
                });
            }
        }

    })
</script>
</html>